import Sound from './AudioPlayer';
import { wordPathEN, wordPathUS } from '../app.config';

export default function readWord(word, type = 'us') {
  const wordURL = {
    us: `${wordPathUS}/${word}.mp3`,
    en: `${wordPathEN}/${word}.mp3`,
  };

  return new Promise((resolve) => {
    const player = new Sound(wordURL[type], null, () => {
      player.play((success) => {
        if (success) {
          console.log('successfully finished playing');
        } else {
          console.log('playback failed due to audio decoding errors');
          // reset the player to its uninitialized state (android only)
          // this is the only option to recover after an error occured and use the player again
          player.reset();
        }
        resolve();
      });
    });
  })
}
